package com.qen.jiaoyu.tangshi

import cn.hutool.core.lang.Console
import cn.hutool.core.util.CharsetUtil
import cn.hutool.poi.excel.ExcelUtil
import it.skrape.core.htmlDocument
import it.skrape.fetcher.BrowserFetcher
import it.skrape.fetcher.Method
import it.skrape.fetcher.response
import it.skrape.fetcher.skrape
import kotlin.collections.forEach

const val TangshiUrl = "https://www.diyifanwen.com/sicijianshang/tangshisanbaishou/"
const val UserAgent =
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36"
const val TangShiExcelPath = "/Users/a1/Documents/Git/Kotlin/Eleven/out/jiaoyu"
fun main() {
    val list = arrayListOf<TangShi>()

    skrape(BrowserFetcher) {
        request {
            url = TangshiUrl
            headers = initHeader()
            cookies = initCookie()
            userAgent = UserAgent
            method = Method.GET
            body = null
            this.timeout = 10000
            charset(CharsetUtil.GBK)
        }
        response {
            htmlDocument {
                ".list-nav-item" {
                    findAll {
                        forEach { it ->
                            it.children {
                                forEach { ul ->
                                    ul.children.forEach { li ->
                                        val author = li.children.get(0).text
                                        val a = li.children.get(1)
                                        val href = a.attribute("href")
                                        Console.log(a.text)
                                        Console.log(a.attribute("href"))
                                        list.add(TangShi(title = a.text, author = author, url = href))
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    Console.log(list.joinToString("\n"))
    val writer = ExcelUtil.getWriter("$TangShiExcelPath/唐诗三百首.xlsx")
    writer.passCurrentRow()
//    writer.merge(quoteList.size - 1, "标题")
    writer.write(list, true)
    writer.close()
}

fun initHeader(): MutableMap<String, String> {
    return mutableMapOf(
        "accept" to "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
        "accept-language" to "zh-CN,zh;q=0.9,en;q=0.8",
        "cache-control" to "no-cache",
        "pragma" to "no-cache",
        "sec-ch-ua" to "\"Google Chrome\";v=\"129\", \"Not=A?Brand\";v=\"8\", \"Chromium\";v=\"129\"",
        "sec-ch-ua-mobile" to "?0",
        "sec-ch-ua-platform" to "\"macOS\"",
        "sec-fetch-dest" to "document",
        "sec-fetch-mode" to "navigate",
        "sec-fetch-site" to "same-origin",
        "sec-fetch-user" to "?1",
        "upgrade-insecure-requests" to "1",
        "accept-encoding" to "utf-8"
    )
}

fun initCookie(): MutableMap<String, String> {
    return mutableMapOf(
        "SetCookieTF" to "1",
        "testcookie" to "yes",
        "Hm_lvt_3a5e11b41af918022c823a8041a34e78" to "1729817592",
        "HMACCOUNT" to "8E7727E55235B30C",
        "DYFWUID" to "17298176038533kc8cgdfyo8q0u6",
        "Hm_lpvt_3a5e11b41af918022c823a8041a34e78" to "1729818055",
    )
}

public data class TangShi(
    val title: String,
    val author: String,
    val url: String,
)